﻿using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Handrise.CDCable.Mobile.Business;
using HandRise.MBHOP.Controls;
using System.Diagnostics;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmLogin :FrmBase
    {
        private UserService userService ;
        private UpdateService updateService ;
        private bool isLogining = false ;
        private bool canNext = true;  //是否能进入下一个页面

        public FrmLogin()
        {
            InitializeComponent();
            ilstdown.Images.Add(ResourceImage.登陆02);
            ilstdown.Images.Add(ResourceImage.首页键盘02);
            ilstdown.Images.Add(ResourceImage.退出02);
            Ilstup.Images.Add(ResourceImage.登陆01);
            Ilstup.Images.Add(ResourceImage.首页键盘01);
            Ilstup.Images.Add(ResourceImage.退出01);
            pictureBox1.Image = ResourceImage.登陆背景;

            AppLogic.loginForm = this;
            AppLogic.Init();
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
            this.ttlbrhead.Visible = false;
            this.ttlbrhead.Visible = false;
            ibtnstar.Visible = false;
            ibtntime.Visible = false;
            pbBattery.Visible = false;
            pbGPS.Visible = false;
            pbSignal.Visible = false;


            userService = new UserService();
            userService.OnLoginComplete += new UserService.LoginCompleteHandler(userService_OnLoginComplete);

            //设置登录用户下拉框
            List<User> users = userService.GetAllUser();

            cbUserName.DataSource = users;

            updateService = new UpdateService();
            updateService.OnUpdateProcess += new UpdateProcessHandler(updateService_OnUpdateProcess);
            //AppLogic.InitScanBar(this);

        }

        private void ShowMainForm()
        {
            FormManage.GotoNext("FrmMain", null,null, false);

        }

        private void bcxunjian_OnButtonClick_1(object sender, int buttonIndex)
        {
            switch (buttonIndex)
            {
                case 2:
                    // PlatformAPI.showTaskBar();
                    MakeFullScreen.toRegularScr(this);
                    Application.Exit();
                    break;
                case 1:
                    InputMethod.SHFullScreen(this.Handle, 4);
                    InputMethod.SipShowIM(1);
                    tbPassword.Focus();
                    break;
                case 0:
                    Login(cbUserName.Text, tbPassword.Text);
                    break;

            }
        }

        private void imageButton6_Click_1(object sender, EventArgs e)
        {
            //MessageBox.Show(AppLogic.netService.CurrentNetType.ToString());

            APNService apnService = new APNService();
            int re = apnService.ConnectAPN();
            switch(re)
            {
                case 0:
                    mssdlgexec.ShowDialog(this, "网络连接成功", DialogType.Confirm, FormManage.form_Deactivate);
                    break;
                case 1:
                    mssdlgexec.ShowDialog(this, "USB连接状态下不能进行APN连接", DialogType.Confirm, FormManage.form_Deactivate);
                    break;
                case -1:
                    mssdlgexec.ShowDialog(this, "网络连接失败", DialogType.Confirm, FormManage.form_Deactivate);
                    break;

            }

        }



        /// <summary>
        /// 用户登录完成
        /// </summary>
        /// <param name="isSuccess"></param>
        /// <param name="user"></param>
        void userService_OnLoginComplete(bool isSuccess, User user)
        {
            isLogining = false;

            if (isSuccess)
            {
                //如果是USB连接，进行升级
                if (AppLogic.netService.CurrentNetType == NetType.USB)
                {
                    if (updateService.QueryNeedUpdate())
                    {
                        if (mssdlgSelect.ShowDialog(this, "系统有新版本，是否升级？", DialogType.Select, FormManage.form_Deactivate) == MessageDialogResult.Button1)
                        {
                            ShowUpdateInfo(true);
                            updateService.StartUpdate();
                        }
                    }
                }

                if (canNext)
                {
                    //启动GPS轨迹跟踪服务
                    if(cbCollectGPS.Checked)
                        AppLogic.gpsTraceService.Start();
                    
                    ShowMainForm();
                }
            }
            else
            {
                mssdlgexec.ShowDialog(this, "登录失败", DialogType.Confirm, FormManage.form_Deactivate);
                return;
            }
        }

        private void Login(string userName, string password)
        {
            if (isLogining)
                return;

            if (userName.Trim() == "")
            {
                mssdlgexec.ShowDialog(this, "用户名不能为空", DialogType.Confirm, FormManage.form_Deactivate);
                return;
            }

            isLogining = true;
            userService.Login(userName, password);
        }

        private void ShowUpdateInfo(bool visible)
        {
            lbUpdate.Visible = visible;
            pbUpdateProcess.Visible = visible;
            pbUpdate.Visible = visible;
        }

        private void updateService_OnUpdateProcess(string info, int percent)
        {
            lbUpdate.Text = info + percent.ToString() + "%";
            pbUpdateProcess.Value = percent;

            if (percent == 100 && info != "下载失败")
            {
                lbUpdate.Text = "下载完成，开始更新";
                
                //下载完成，关闭程序，并启动更新程序
                Process.Start(AppLogic.GetLocalPath() + "\\Handrise.CDCable.Update.exe", updateService.ZipFileLen.ToString() + " " + updateService.ServerVersion);

                Application.Exit();

                canNext = false;
            }

            Refresh();
        }


    }
}